package com.chaosj.tools

import java.lang.management.ManagementFactory
import java.nio.file.Files
import java.nio.file.Paths
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.Phaser
import java.util.stream.Collectors

/**
 * @Description:
 * @author: caoyangjie
 * @date: 2019年11月2019/11/26日 10:21
 * @Copyright: © 赢时胜
 */
class TestFileSearchFilter {
    static void main(args){
        Queue<String> concurrentSet = new ConcurrentLinkedQueue<>()
        Queue<String> serviceBusSubSet = new ConcurrentLinkedQueue<>()
        List<String> filePath = getSearchPaths()
        int canUseCPU = ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors()
        def phaser = new Phaser(canUseCPU>filePath.size()?filePath.size():canUseCPU)
        filePath.parallelStream().forEach({path->
            def fs = new FileSearchFilter(path, phaser, {file ->
                try {
                    // 所有的接口清单
//                    return file.getName().endsWith("java") && Files.readAllLines(file.toPath()).stream().limit(50).filter({line->line.startsWith("public interface")}).findAny().isPresent();
                    // 所有的枚举清单
//                    return file.getName().endsWith("java") && Files.readAllLines(file.toPath()).stream().limit(50).filter({line->line.startsWith("public enum")}).findAny().isPresent();
                    // 所有的实现了 ServiceBus 的类
//                    return file.getName().endsWith("java") && Files.readAllLines(file.toPath()).stream().limit(50).filter({line->
//                        if( line.contains(" extends ServiceBus<") ){
//                            return true
//                        }
//                        return serviceBusSubSet.stream().filter({sub->line.contains(" extends "+sub)}).findFirst().isPresent()
//                    }).findAny().isPresent()
                    // 所有的实现了 sqlbuilder 的类清单
//                    return file.getName().endsWith("java") && Files.readAllLines(file.toPath()).stream().filter({ line -> line.contains("implements SQLBuilder")}).findAny().isPresent()
                    // 所有的实现了 BasePojo 的类清单
                    return file.getName().endsWith("java") && Files.readAllLines(file.toPath()).stream().limit(50).filter({line->
                        line.contains(" extends BasePojo")
                    }).findAny().isPresent()
                } catch (IOException e) {
                }
                return false
            } ,{q,f->
                q.add(f.getAbsoluteFile().getAbsolutePath())
            })
            fs.run ( )
            concurrentSet.addAll( fs.getResults() )
        })
        concurrentSet.stream().sorted().distinct().forEach({k->println(k)})
        System.out.println("-----------------------------------------------------")
        System.out.println("-----------------------------------------------------")
        System.out.println("-----------------------------------------------------")
        System.out.println("-----------------------------------------------------")
        System.out.println("-----------------------------------------------------")
        System.out.println("-----------------------------------------------------")
        System.out.println("-----------------------------------------------------")
        System.out.println("-----------------------------------------------------")
        System.out.println("-----------------------------------------------------")
        System.out.println("-----------------------------------------------------")
        serviceBusSubSet.stream().sorted().forEach({k->println(k)})
    }

    static List<String> getSearchPaths() throws IOException {
        List<String> contains = null
//        contains = Arrays.asList("com.yss.communicationProtocol","com.yss.fast.atomicdata","com.yss.fast.systemmanager")
        return Files.list(Paths.get("E:\\platform\\workspaces\\Code\\server\\source"))
                .filter({ path -> path.toFile().isDirectory()})
                .filter({ path->
                    (contains==null || contains.isEmpty())?true:contains.stream().filter({k->path.toFile().getAbsolutePath().contains(k)}).findAny().isPresent()
                })
                .map({ path -> path.toFile().getAbsolutePath()}).collect(Collectors.toList())
    }
}
